<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 系统错误捕获
 * 需开启application.dispatcher.catchException =1后生效
 */
use config\Response;

class ErrorController extends \Yaf_Controller_Abstract{

    /**
     * @param $exception
     *配合application.dispatcher.catchException =1使用, 可以捕获抛出的错误
     */
    function errorAction($exception){
        $code = $exception->getCode();
        $message = $exception->getMessage();
        $file = $exception->getFile(); // 报错文件
        $line = $exception->getLine(); // 文件报错位置的行号
        $trace = $exception->getTrace(); // 报错信息，数组格式
        $traceAsString = $exception->getTraceAsString(); // 报错信息字符串

        $log = "[" . date('Y-m-d H:i:s') . "]\r\n";
        $log .= "请求地址: " . $_SERVER['REQUEST_URI'] . "\r\n";
        $log .= "请求参数：" . http_build_query($_REQUEST) . "\r\n";
        $log .= "错误码： " . $code . "\r\n";
        $log .= "错误信息: " . $message . "\r\n";
        $log .= "报错文件: " . $file . "\r\n";
        $log .= "报错行号: " . $line . "\r\n";
        $log .= "Trace: \r\n" . $traceAsString . "\r\n\r\n";

        Log::error($log);

        if(DEBUG_MODE){
            die($log);
        }else{
            response(Response::UNCATCH_EXCEPTION);
        }
    }
}




